import fs from 'fs';
import path from 'path';
import https from 'https';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const files = [
  {
    url: 'https://unpkg.com/@ffmpeg/core@0.12.4/dist/esm/ffmpeg-core.js',
    path: '../public/ffmpeg-core.js'
  },
  {
    url: 'https://unpkg.com/@ffmpeg/core@0.12.4/dist/esm/ffmpeg-core.wasm',
    path: '../public/ffmpeg-core.wasm'
  },
  {
    url: 'https://unpkg.com/@ffmpeg/core@0.12.4/dist/esm/ffmpeg-core.worker.js',
    path: '../public/ffmpeg-core.worker.js'
  }
];

files.forEach(file => {
  const filePath = path.join(__dirname, file.path);
  const dir = path.dirname(filePath);
  
  if (!fs.existsSync(dir)) {
    fs.mkdirSync(dir, { recursive: true });
  }
  
  https.get(file.url, (response) => {
    const writeStream = fs.createWriteStream(filePath);
    response.pipe(writeStream);
    
    writeStream.on('finish', () => {
      console.log(`Downloaded: ${file.path}`);
    });
  }).on('error', (err) => {
    console.error(`Error downloading ${file.url}:`, err);
  });
}); 